((window) => {
    // 定义状态值常量
    const PENDING = 'pending'
    const FULFILLED = 'fulfilled'
    const REJECTED = 'rejected'

    function Promise(excutor) {
        this.state = PENDING
        this.data = undefined
        this.callbacks = []
        const self = this

        function resolve(value) {
            if (self.state !== PENDING) return

            self.state = FULFILLED
            self.data = value

            setTimeout(() => {
                self.callbacks.forEach(callbcakObj => callbcakObj.resolve(value))
            }, 0)
        }

        function reject(reason) {
            if (self.state !== PENDING) return

            self.state = REJECTED
            self.data = reason

            setTimeout(() => {
                self.callbacks.forEach(callbcakObj => callbcakObj.resolve(reason))
            }, 0)
        }

        try {
            excutor(resolve, reject)
        } catch (error) {
            reject(error)
        }

    }

    Promise.prototype.then = function (onResolved, onRejected) {
        const self = this

        onResolved = typeof onResolved === 'function' ? onResolved : value => value
        onRejected = typeof onRejected === 'function' ? onRejected : reason => {throw reason}

        return new Promise((resolve, reject) => {
            function handle(callback) {
                try {
                    const result = callback(self.data)
                    if (result instanceof Promise) {
                        result.then(
                            value => resolve(value),
                            reason => reject(reason)
                        )
                    } else {
                        resolve(result)
                    }
                } catch (error) {
                    reject(error)
                }
            }

            if (self.state === FULFILLED) {
                setTimeout(() => {
                    handle(onResolved)
                }, 0)
            } else if (self.state === REJECTED) {
                setTimeout(() => {
                    handle(onRejected)
                }, 0)
            } else {
                self.callbacks.push({
                    resolved: () => handle(onResolved),
                    rejected: () => handle(onRejected)
                })
            }
        })
    }

    Promise.prototype.catch = function (onRejected) {
        return this.then(value => value, onRejected)
    }

    Promise.resolve = function (value) {
        return new Promise((resolve, reject) => {
            if (value instanceof Promise) {
                value.then(
                    value => resolve(value),
                    reason => reject(reason)
                )
            } else {
                resolve(value)
            }
        })
    }

    Promise.reject = function (reason) {
        return new Promise((resolve, reject) => {
            reject(reason)
        })
    }

    Promise.all = function (promises) {
        const values = []

        let resolvedCount = 0
        return new Promise((resolve, reject) => {

        })
    }

    Promise.race = function (promises) {
        
    }
    
    Promise.allSettled = function (promises) {
        
    }








    window.Promise = Promise
})(window);